<?php
namespace App\Http\Controllers\MobileWeb;

use App\Http\Controllers\WebController;
use App\Http\Controllers\Business\App\PublicBusiness;
use App\Http\Controllers\Business\App\Today\Today;
use App\Http\Controllers\Business\App\Common\Common;
use App\Http\Controllers\Business\App\Auth\AppAuth;
use App\Http\Controllers\Business\App\MyCenter\MyCenter;
use Illuminate\Support\Facades\Cookie;
use Illuminate\Support\Facades\Auth;

class CommonController extends WebController
{
    //翻倍轨迹
    public function course() {
        $userAgent = $this->request->header('user-agent');
        $platform = PublicBusiness::getPlatformByUserAgent($userAgent);
        $avatar = $this->request->get('avatar');
        if(!strstr($avatar, 'http')) {
            $avatar = env('IMAGE_DOMAIN') . $avatar;
        }
        $nickname = $this->request->get('nickname');
        $sumday = $this->request->get('sumday');
        $sumday = ($sumday == 0)?1:$sumday;
        $sumrechargemoney = $this->request->get('sumrechargemoney');
        $sumordermoney = $this->request->get('sumordermoney');;
        $savemoney = $this->request->get('savemoney');
        $createdat = $this->request->get('createdat');
        $firstrechargetime = $this->request->get('firstrechargetime');
        $firstordertime = $this->request->get('firstordertime');
        $firstshopname = $this->request->get('firstshopname');
        if(stristr($firstshopname, '?noncestr')) {
            $firstshopname = substr($firstshopname, 0, strpos($firstshopname, '?noncestr'));  // 返回?noncestr之前的子字符串
        } else {
            $firstshopname = $firstshopname;
        }
        $data = [
            'platform' => $platform,
            'avatar' => $avatar,
            'nickname' => $nickname,
            'sumday' => $sumday,
            'sumrechargemoney' => $sumrechargemoney,
            'sumordermoney' => $sumordermoney,
            'savemoney' => $savemoney,
            'createdat' => date('Y-m-d H:i:s', strtotime($createdat)),
            'firstrechargetime' => ($firstrechargetime!='0000-00-00 00:00:00')?date('Y-m-d H:i:s', strtotime($firstrechargetime)):'',
            'firstordertime' => ($firstordertime!='0000-00-00 00:00:00')?date('Y-m-d H:i:s', strtotime($firstordertime)):'',
            'firstshopname' => $firstshopname,
        ];
        return view('mobile.wallet.course', $data);
    }

    //推荐有奖的分享
    public function shareRecommend() {
        $parentId = $this->request->get('parentid');
        $data = [
            'parentid' => $parentId
        ];
        return view('mobile.share.share_recommend', $data);
    }
    //推荐有奖，用户输入手机后提交
    public function submitRecommend() {
        $newParentId = $this->request->get('parentid');
        $mobile = $this->request->get('mobile');
        $parentId = PublicBusiness::encrypt($newParentId, 'D', '');
        if($parentId == '') {
            //$parentId = urldecode($parentId);
            $parentId = rawurldecode($newParentId);
            $parentId = PublicBusiness::encrypt($parentId, 'D', '');
        }
        $result = MyCenter::recommend($parentId, $mobile, 'doublecoupon');
        if($result['status'] == 'failed') {
            return $this->responseError($result['message']);
        } else {
            return $this->responseSuccessMessage($result['message']);
        }
    }
    //充值的分享
    public function shareRecharge() {
        if($this->request->has('platform')) {
            $platform = $this->request->get('platform');
        } else {
            $platform = 'web'; //默认是web端
        }
        $system = '';
        $userAgent = $this->request->header('user-agent');
        if(strstr($userAgent, "FBWiOSApp")) {
            $platform = "iOS";
        } elseif(strstr($userAgent, "FBWAndroidApp")) {
            $platform = "Android";
        } elseif(stripos($userAgent, 'Android')) {
            $platform = 'Android';
        } elseif(stripos($userAgent, "iphone")) {
            $platform = "iOS";
        } elseif(stripos($userAgent, "ipad")) {
            $platform = "iOS";
        }
        if(stripos($userAgent, 'MicroMessenger')) {
            $isweixin = '1';
        } else {
            $isweixin = '0';
        }
        if(stripos($userAgent, 'MicroMessenger')) {
            $isweixin = '1';
        } else {
            $isweixin = '0';
        }
        $androidDownloadUrl = 'http://file.fbw-china.com/download/fbw.apk';
        $iosDownloadUrl = 'https://itunes.apple.com/cn/app/id1172353317'; //TODO ios 下载
        $data = [
            'system' => $system,
            'platform' => $platform,
            'isweixin' => $isweixin,
            'iosdownloadurl' => $iosDownloadUrl,
            'androiddownloadurl' => $androidDownloadUrl,
        ];
        return view('mobile.share.share_recharge', $data);
    }
}